package com.tintuc24h.com;

import com.tintuc24h.untils.Variables;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class News extends Activity {
	private WebView webview;
	private String link;
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news);
		Bundle bundle = getIntent().getExtras();
		link = bundle.getString(Variables._LINK);

		webview = (WebView) findViewById(R.id.webview);

		webview.getSettings().setSupportZoom(true);
		webview.setInitialScale(1);
		webview.getSettings().setLoadWithOverviewMode(true);
		webview.getSettings().setUseWideViewPort(true);
		webview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
		webview.setScrollbarFadingEnabled(false);
		webview.setWebViewClient(new WebviewClient());
		if(isOnline()==true){
		dialog = ProgressDialog.show(this, "", "Loading...");
		new NewTask().execute();
		}
		else {
			Toast.makeText(this, "Hay ket noi mang internet", Toast.LENGTH_LONG).show();
		}
	}

	private Boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni != null && ni.isConnected()) {
			return true;
		}
		return false;
	}

	class NewTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			webview.loadUrl(link);
			return null;
		}

	}

	class WebviewClient extends WebViewClient {

		@Override
		public void onPageFinished(WebView view, String url) {
			if (dialog != null) {
				dialog.dismiss();
			}
			super.onPageFinished(view, url);
		}

	}

}
